<template>
  <div>

    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>品牌管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增品牌</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <el-form 
      style="width: 500px" 
      ref="form"
      :rules="rules"
      :model="form" label-width="100px">
      <el-form-item label="品牌名称" prop="brandName">
        <el-input v-model="form.brandName"/>
      </el-form-item>
      <el-form-item label="品牌logo" prop="brandAvatar">
        <el-upload
          class="avatar-uploader"
          action=""
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="form.brandAvatar" :src="form.brandAvatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">立即新增</el-button>
        <el-button>重置</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
  import myaxios from '@/http/MyAxios'

  export default {
    data() {
      return {
        form: {
          brandName: '', 
          brandAvatar: ''
        },
        rules: {
          brandName: [
            {required:true, message:'该字段必填', trigger: 'blur'}
          ], 
          brandAvatar: [
            {required:true, message:'该字段必填', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {

      /** 提交表单 */
      submit(){
        this.$refs.form.validate(valid=>{
          if(valid){
            console.log('验证通过')
            // 发送请求, 执行新增业务
            let url = ""
            myaxios.post(url, this.form).then(res=>{
              console.log('新增品牌结果', res)
              if(res.data.code==200){
                this.$message({
                  message: '品牌新增成功',
                  type: 'success'
                })
                // 重置表单
                this.$refs.form.resetFields()
              }else{
                this.$message.error('新增失败, 请稍后重试')
              }
            })
          }else{
            console.log('验证失败')
          }
        })
      },
      
      /** 上传成功后执行 */
      handleAvatarSuccess(res, file) {
        console.log(res)
        this.form.brandAvatar = res.data
      },

      /** 上传文件的格式 / 大小验证 */
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      }
    },
  }
</script>

<style lang="scss" scoped>

</style>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 128px;
    height: 128px;
    line-height: 128px;
    text-align: center;
  }
  .avatar {
    width: 128px;
    display: block;
  }
</style>
